package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.OtherStudentDao;
import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    // 1. 创建 StudentDao 对象
    private OtherStudentDao studentDao = new OtherStudentDao();

    public boolean addStudent(Student stu) {
        // 2. 将学生对象，传递给 StudentDao 中的 addStudent 方法
        // 3. 将返回的 boolean 类型结果，返回给 StudentController
        return studentDao.addStudent(stu);
    }

    public boolean isExists(String id) {
        Student[] stus = studentDao.findAllStudent();
        // 假设 id 在数组中不存在
        boolean exists = false;
        // 遍历数组，获取每一个学生对象，准备进行判断
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student != null && student.getId().equals(id)) {
                exists = true;
                break;
            }
        }

        return exists;
    }

    public Student[] findAllStudent() {
        Student[] stus = studentDao.findAllStudent();
        boolean flag = false;
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            return stus;
        } else {
            return null;
        }
    }

    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }

    public boolean updateStudent(Student stu) {
        studentDao.updateStudent(stu);
        return true;
    }

    public void updateStudent(String updateId, Student stu) {
        studentDao.updateStudent(updateId, stu);
    }
}
